<link type="text/css" rel="stylesheet" href="dhtml_calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
	<SCRIPT type="text/javascript" src="dhtml_calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.js?random=20060118"></script>

<!-- End line for JS CALENDER -->

<?php

include("utils.php");
//=====================================================language=============================================
if($_SESSION['langname']=="")
{
	$language_url="languages/"."english".'/update_campaign_add_language.php';
}
else
{
	$language_url="languages/".$_SESSION['langname'].'/update_campaign_add_language.php';
}

include_once("$language_url");
//======================================================End============================================
include("lang_buildCustomNetwork.php");
ob_start();
session_start();
if(!isset($_SESSION[siteuserId]))
echo '<script>location.href="index.php"</script>';

main_content("mainBuySpace();");

function mainBuySpace()
{
if($_SESSION['langId']!="")
{
	$langId=$_SESSION['langId'];
}
else
{
	$langId="1";
}



//Getting all data from table

$radio = $_GET['radio'];
	
	//retrieving data for campaign//////////////////
$query_retrieve_campaign = "select * from adcampaign where campaign_id = '".$radio."' "; 
$res_retrieve_campaign = mysql_query($query_retrieve_campaign);
$row_retrieve_campaign = mysql_fetch_array($res_retrieve_campaign);

	//retrieving data for adgroup//////////////////
$query_retrieve_adgroup = "select * from adgroup where campaign_id = '".$radio."'"; 
$res_retrieve_adgroup = mysql_query($query_retrieve_adgroup);

//End of getting all data from table


if(isset($_POST['help_update_main']))
{

$query_update_campaign = "UPDATE adcampaign SET camp_name='".$_POST['camp_name']."',spend_memo='".$_POST['spend_memo']."',total_budget='".$_POST['total_budget']."',daily_budget='".$_POST['daily_budget_main']."',start_date='".$_POST['start_date']."',end_date='".$_POST['end_date']."',automatic_charges='".$_POST['auto_charge']."',charges_amount='".$_POST['charges_amt']."',start_hour='".$_POST['start_date_hour']."',end_hour='".$_POST['end_date_hour']."' where campaign_id = '".$radio."' and siteuserId='".$_SESSION[siteuserId]."' ";

mysql_query($query_update_campaign);

$total_adgroup = $_POST['total_adgroup'];


for($i=1;$i<$total_adgroup;$i++)
{
if($_POST['network_'.$i] == 'custom')
{
	
	$chkNetworkROW = mysql_query("SELECT adgroup_network,network_type FROM adgroup WHERE adgroup_id = '".$_POST['add_gr_id_'.$i]."'") or die('error');
	if(mysql_num_rows($chkNetworkROW) > 0)
	{
		
		$chkNetworkRS = mysql_fetch_array($chkNetworkROW);
		$adgroup_network = $chkNetworkRS['adgroup_network'];
		$network_type = $chkNetworkRS['network_type'];
	}
//SELECT network_id FROM create_network WHERE network_id = '1' AND userid = '7'

	$chkCustomROW = mysql_query("SELECT network_id FROM create_network WHERE network_id = '".$adgroup_network."' AND userid = '".$_SESSION['siteuserId']."'") or die('error');
	if(mysql_num_rows($chkCustomROW) == 0 && $network_type != 'custom')
	{

		//die();
		/************* INSERT INTO create_network TABLE*****************/
		$tem_cr_net_query = "select * from temp_create_network where counter = '".$i."' and type = 'db' ";
		$tem_cr_net_res   = mysql_query($tem_cr_net_query);
		if(mysql_num_rows($tem_cr_net_res)>0)
		{
			$tem_cr_net_row = mysql_fetch_array($tem_cr_net_res);
			$query_temp = "INSERT INTO create_network SET `ad_type` = '".$tem_cr_net_row['ad_type']."',`bid`='".$tem_cr_net_row['bid']."',`per`='".$tem_cr_net_row['per']."',`network`='".$tem_cr_net_row['network']."',`banner_size`='".$tem_cr_net_row['banner_size']."',`category`='".$tem_cr_net_row['category']."',`country`='".$tem_cr_net_row['country']."',`userid` = ".$_SESSION['siteuserId']."";
			//die();
			mysql_query($query_temp) or die('error in insert into create network');
			$lastinsertid = mysql_insert_id();
			$query_update_adgroup = "UPDATE adgroup SET adgroup_name='".$_POST['add_gr_name_'.$i]."',adgroup_daily_budget='".$_POST['daily_budget_'.$i]."',network_type='custom',adgroup_network = '".$lastinsertid."' where adgroup_id='".$_POST['add_gr_id_'.$i]."' ";
			mysql_query($query_update_adgroup);
		}
	}
	else
	{
		//die();
		/************* UPDATE  create_network TABLE *****************/

		$tem_cr_net_query = "select * from temp_create_network where counter = '".$i."' and type = 'db' ";
		$tem_cr_net_res   = mysql_query($tem_cr_net_query);
		
		if(mysql_num_rows($tem_cr_net_res)>0)
		{
		$tem_cr_net_row = mysql_fetch_array($tem_cr_net_res);
		
		$query_temp = "UPDATE create_network SET `ad_type` = '".$tem_cr_net_row['ad_type']."',`bid`='".$tem_cr_net_row['bid']."',`per`='".$tem_cr_net_row['per']."',`network`='".$tem_cr_net_row['network']."',`banner_size`='".$tem_cr_net_row['banner_size']."',`category`='".$tem_cr_net_row['category']."',`country`='".$tem_cr_net_row['country']."',`userid` = ".$_SESSION['siteuserId']." where network_id= '".$tem_cr_net_row['identity']."' ";
		mysql_query($query_temp);
		
		$query_update_adgroup = "UPDATE adgroup SET adgroup_name='".$_POST['add_gr_name_'.$i]."',adgroup_daily_budget='".$_POST['daily_budget_'.$i]."',network_type='custom',adgroup_network = '".$tem_cr_net_row['identity']."' where adgroup_id='".$_POST['add_gr_id_'.$i]."' ";
		mysql_query($query_update_adgroup);
		}
		

	}
	
}
else{

	$selCustom = "select * from adgroup where adgroup_id='".$_POST['add_gr_id_'.$i]."' and network_type='custom'";
		
	$isCustom = mysql_query($selCustom);
	if(mysql_num_rows($isCustom) >0)
	{
		$getCustomid = mysql_fetch_array($isCustom);
		$delQuery = "delete from create_network where network_id='".$getCustomid['adgroup_network']."'";
		mysql_query($delQuery);
	}
	 $query_update_adgroup = "UPDATE adgroup SET adgroup_name='".$_POST['add_gr_name_'.$i]."',adgroup_daily_budget='".$_POST['daily_budget_'.$i]."',adgroup_network='".$_POST['network_'.$i]."',network_type='direct' where adgroup_id='".$_POST['add_gr_id_'.$i]."' ";
	mysql_query($query_update_adgroup);
}

//Retrieving New Added Ad in existing Adgroup

$query_retrieve_adgroup_ad = "select * from temp_adgroup_ad where form_id = ".$i." ";
$res_retrieve_adgroup_ad   = mysql_query($query_retrieve_adgroup_ad);

if(mysql_num_rows($res_retrieve_adgroup_ad) > 0)
{
	while($row_retrieve_adgroup_ad = mysql_fetch_array($res_retrieve_adgroup_ad))
	{
		$que = "insert into adgroup_ad(adgroup_id,heading,ad_description,dest_url,personal_reference,campaign_id) values('".$_POST['add_gr_id_'.$i]."','".$row_retrieve_adgroup_ad['heading']."','".$row_retrieve_adgroup_ad['ad_description']."','".$row_retrieve_adgroup_ad['dest_url']."','".$row_retrieve_adgroup_ad['personal_reference']."','".$radio."') " ;

		mysql_query($que);
	}
mysql_query("delete from temp_adgroup_ad where form_id = ".$i." ");

}



}

//die(); //main die

$total_new_adgroup = $_POST['TOTAL_FILE'];


for($i=1;$i<$total_new_adgroup;$i++)
{

$temp_query = "select * from temp_create_network where identity = '".$i."' and type = 'dynamic' ";
$temp_res   = mysql_query($temp_query);
$temp_row   = mysql_fetch_array($temp_res);

if(mysql_num_rows($temp_res)>0)
{

$create_network_query = "INSERT INTO create_network(`ad_type`, `bid`, `per`, `network`, `banner_size`, `category`, `country`, `userid`) values('".$temp_row['ad_type']."','".$temp_row['bid']."','".$temp_row['per']."','".$temp_row['network']."','".$temp_row['banner_size']."','".$temp_row['category']."','".$temp_row['country']."','".$temp_row['userid']."') "; 

$create_network_res = mysql_query($create_network_query);
$create_network_insertid = mysql_insert_id();

// End Dynamic adgroup's network insert from temp_create_network table

 $query_campaign_adgroup = "insert into adgroup(adgroup_name,adgroup_daily_budget,adgroup_network,siteuserId,campaign_id,network_type) values('".$_POST['add_gr_name'.$i]."','".$_POST['daily_budget'.$i]."','".$create_network_insertid."','".$_SESSION[siteuserId]."','".$radio."','custom') ";

}

else
{
 $query_campaign_adgroup = "insert into adgroup(adgroup_name,adgroup_daily_budget,adgroup_network,siteuserId,campaign_id,network_type) values('".$_POST['add_gr_name'.$i]."','".$_POST['daily_budget'.$i]."','".$_POST['network'.$i]."','".$_SESSION[siteuserId]."','".$radio."','direct') ";

}


$res_campaign_adgroup = mysql_query($query_campaign_adgroup);
$new_added_adgroup_id = mysql_insert_id();


$form_ret_id = "update_".$i;
$query_retrieve_new_added_adgroup_ad = "select * from temp_adgroup_ad where form_id = '".$form_ret_id."' ";
$res_retrieve_adgroup_ad   = mysql_query($query_retrieve_new_added_adgroup_ad);
$mysql_num_rows = mysql_num_rows($res_retrieve_adgroup_ad);
if($mysql_num_rows > 0)
{
	while($row_retrieve_adgroup_ad = mysql_fetch_array($res_retrieve_adgroup_ad))
	{
		$que = "insert into adgroup_ad(adgroup_id,heading,ad_description,dest_url,personal_reference,campaign_id) values('".$new_added_adgroup_id."','".$row_retrieve_adgroup_ad['heading']."','".$row_retrieve_adgroup_ad['ad_description']."','".$row_retrieve_adgroup_ad['dest_url']."','".$row_retrieve_adgroup_ad['personal_reference']."','".$radio."') " ;

		mysql_query($que);
	}
}

}

$delete = "delete from temp_adgroup_ad where siteuserId = '".$_SESSION[siteuserId]."' ";
mysql_query($delete);
mysql_query("delete from temp_create_network where userid = '".$_SESSION[siteuserId]."' ");

}

//die();


// Start Retrieve network List

$sqlNetworkAds = "select * from ".NETWORKMASTER.",".NETWORKDETAILS." where ".NETWORKMASTER.".status='Y' AND ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId";
$execNetworkAds = mysql_query($sqlNetworkAds);
$execNetworkAds1 = mysql_query($sqlNetworkAds);
// End Retrieve network List

$optionstring='';
while($fetNetworkAds1 = mysql_fetch_array($execNetworkAds1))
{ 
	$optionstring .= '<option value='.$fetNetworkAds1[networkId].'>'.strtoupper($fetNetworkAds1[networkType]).'-'.ucfirst($fetNetworkAds1[networkName]).'</option>';
}
?>

<script type="text/javascript">
var gFiles = 1;
   function callOpen(id)
{
window.open('ad_adgroup_update.php?ad_id='+id,'popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false;
}

   function callOpenNewAdgroup(id)
{
window.open('ad_adgroup_newly_added_update.php?ad_id='+id,'popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false;
}

   function callOpenExistingAdgroup(id)
{
window.open('main_adgroup_new_ad_update.php?ad_id='+id,'popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false;
}

function delete_garbage_adgrp()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
	delete_garbage_network();
// alert('Going');

//alert(xmlHttp.responseText);	  
      }
    }

  xmlHttp.open("GET","delete_temp_adgroup.php",true);
  xmlHttp.send(null);
  return false; 
}


function delete_garbage_network()
{
/*alert('Coming');*/
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
/*alert('yes');*/
	//alert(xmlHttp.responseText);	  
      }
    }

  xmlHttp.open("GET","delete_garbage_network.php",true);
  xmlHttp.send(null);
  return false; 
}

function make_div(gFiles)
{

window.open('advertiser_direct_new_adgroup_update.php?form_id='+ gFiles +'' ,'popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false;
}



function adddiv(optionstring) 
{ 

    var tr = parent.document.createElement('tr');
    tr.setAttribute('id', 'adgroup' + gFiles);
    var td = parent.document.createElement('td');
    var subb = "subb";
    maintdbody = '<table width="100%" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                  <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11" /></td>
                                  <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11" /></td>
                                  <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11" /></td>
                                </tr>
                            </table></td>
                          </tr>
                          <tr>
                            <td align="center" class="PinkTwosideBorder"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
                                <tr>
                                  <td align="center"><table name="createad'+gFiles+'" id="createad'+gFiles+'"><TR><TD>Ad Type: Text</TD><td>Units: CPC</td></TR><TR><TD>Ad Group Name</TD><td><input type="text" name="add_gr_name'+gFiles+'" id="add_gr_name'+gFiles+'"></td></TR><TR><TD></TD><td id="validate_add_gr_name'+gFiles+'" style="display:none;"></td></TR><TR><TD>Daily Budget</TD><td><input type="text" name="daily_budget'+gFiles+'" id="daily_budget'+gFiles+'"></td></TR><TR><TD>Network</TD><td><select name="network'+gFiles+'"  onchange="return custom_check_dynamic('+gFiles+');" id="network'+gFiles+'"><option value="Select Network">Select Network</option><option value="custom">Custom</option>';

    maintdbody +=optionstring;

    maintdbody += '</select></td></TR><TR><TD></TD><td id="validate_network'+gFiles+'" style="display:none;"></td></TR><TR><TD id="tdCreateAdUpdate_'+gFiles+'" colspan="2"></TD></TR><TR><TD></TD><td><input type="button" name="create_ad'+gFiles+'" id="create_ad'+gFiles+'" value="Create Ad" onclick ="make_div(' + gFiles + ')" ></td></TR></table></td>
                                </tr>
                            </table></td>
                          </tr>
                          <tr>
                            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                  <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11" /></td>
                                  <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11" /></td>
                                  <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11" /></td>
                                </tr>
                            </table></td>
                          </tr>
                        </table>';

    td.innerHTML = maintdbody;
    tr.appendChild(td);
    parent.document.getElementById('adgroup').appendChild(tr);
    parent.document.getElementById('adgroup').innerHTML = parent.document.getElementById('adgroup').innerHTML;
	gFiles++;
  	parent.document.getElementById('TOTAL_FILE').value=gFiles;
	//alert(document.getElementById('TOTAL_FILE').value);
	
}


function removeFile(aId) 
{
    var obj = document.getElementById(aId);
    obj.parentNode.removeChild(obj);
//alert(gFiles);
}



function ading_adgroup(div_id)
{
var div_id = div_id;

var ad_gr_name = document.getElementById('add_gr_name'+div_id).value;
var dail_bud = document.getElementById('daily_budget'+div_id).value;
var network = document.getElementById('network'+div_id).value;


var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {

//alert(xmlHttp.responseText);	  
      }
    }

  xmlHttp.open("GET","adgroup_ajax.php?ad_gr_name="+ad_gr_name+"&dail_bud="+dail_bud+"&network="+network,true);
  xmlHttp.send(null);
  return false; 
}


function add_main()
{

var ad_gr_name = document.getElementById('add_gr_name').value;
var dail_bud = document.getElementById('daily_budget').value;
var network = document.getElementById('network').value;

var xmlHttp;

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
        document.getElementById("main_adgroup_save").style.display="block";
        document.getElementById("main_adgroup_save").innerHTML="Successfully Saved";
//alert(xmlHttp.responseText);
      }
    }

  xmlHttp.open("GET","add_main_ajax.php?ad_gr_name="+ad_gr_name+"&dail_bud="+dail_bud+"&network="+network,true);
  xmlHttp.send(null);
  return false; 
}




function validate_main_campaign()
{

///Start of Main Campaign Validation/////////////////

if(camp_name != "")    		{
        document.getElementById("validate_camp_name").style.display="none";
    }

if(total_budget != "")   	{
        document.getElementById("validate_total_budget").style.display="none";
    }

if(daily_budget_main != "")     {
        document.getElementById("validate_daily_budget_main").style.display="none";
    }

if(start_date != "")    	{
        document.getElementById("validate_start_date").style.display="none";
    }

if(charges_amt != "")    	{
        document.getElementById("validate_charges_amt").style.display="none";
    }


var camp_name = document.getElementById("camp_name").value;
if(camp_name == "")    {

        document.getElementById("validate_camp_name").style.display="block";
        document.getElementById("validate_add_gr_name").innerHTML="<?php echo ENTER_GROUP_NAME; ?>";
        document.getElementById("camp_name").focus();
        return false;
    }


var total_budget = document.getElementById("total_budget").value;
if(total_budget == "")    {

        document.getElementById("validate_total_budget").style.display="block";
        document.getElementById("validate_add_gr_name").innerHTML="<?php echo ENTER_TOTAL_BUDGET; ?>";
        document.getElementById("total_budget").focus();
        return false;
    }


var daily_budget_main = document.getElementById("daily_budget_main").value;
if(daily_budget_main == "")    {

        document.getElementById("validate_daily_budget_main").style.display="block";
        document.getElementById("validate_daily_budget_main").innerHTML="<?php echo ENTER_DAILY_BUDGET; ?>";
        document.getElementById("daily_budget_main").focus();
        return false;
    }


var start_date = document.getElementById("start_date").value;
if(start_date == "")    {

        document.getElementById("validate_start_date").style.display="block";
        document.getElementById("validate_start_date").innerHTML="<?php echo ENTER_START_DATE; ?>";
        document.getElementById("start_date").focus();
        return false;
    }

var charges_amt = document.getElementById("charges_amt").value;
if(charges_amt == "")    {

        document.getElementById("validate_charges_amt").style.display="block";
        document.getElementById("validate_charges_amt").innerHTML="<?php echo ENTER_CHARGES_AMNT; ?>";
        document.getElementById("charges_amt").focus();
        return false;
    }


///End of Main Campaign Validation/////////////////



//Start of Existing Ad Group Validation///////////

var counter = document.getElementById('total_adgroup').value;

for(i=1;i<counter;i++) {
var ad_main_group = document.getElementById("add_gr_name_"+i).value;
var network = document.getElementById("network_"+i).value;

if(network != "Select Network")    {
        document.getElementById("validate_network_"+i).style.display="none";
    }

if(ad_main_group != "")    {
        document.getElementById("validate_add_gr_name_"+i).style.display="none";
    }



if(ad_main_group == "")    {

        document.getElementById("validate_add_gr_name_"+i).style.display="block";
        document.getElementById("validate_add_gr_name_"+i).innerHTML="<?php echo ENTER_GROUP_NAME; ?>";
        document.getElementById("add_gr_name_"+i).focus();
        return false;
    }

    

   if(network == "Select Network")    {
        document.getElementById("validate_network_"+i).style.display="block";
        document.getElementById("validate_network_"+i).innerHTML="<?php echo ENTER_NETWORK; ?>";
        document.getElementById("network_"+i).focus();
        return false;
    }

}

//End of Existing Ad Group Validation///////////

// Start of New Added Ad Group /////////////////

for(i=1;i<gFiles;i++)
{

if(document.getElementById("createad"+i))
{

var ad_main_group = document.getElementById("add_gr_name"+i).value;
var network = document.getElementById("network"+i).value;


if(ad_main_group != "")    {
        document.getElementById("validate_add_gr_name"+i).style.display="none";
    }

if(network != "Select Network")    {
        document.getElementById("validate_network"+i).style.display="none";
    }

if(ad_main_group == "")    {
        document.getElementById("validate_add_gr_name"+i).style.display="block";
        document.getElementById("validate_add_gr_name"+i).innerHTML="<?php echo ENTER_GROUP_NAME; ?>";
        document.getElementById("add_gr_name"+i).focus();
        return false;
    }

    

   if(network == "Select Network")    {
        document.getElementById("validate_network"+i).style.display="block";
        document.getElementById("validate_network"+i).innerHTML="<?php echo ENTER_NETWORK; ?>";
        document.getElementById("network"+i).focus();
        return false;
    }

}

}

// End of New Added Ad Group /////////////////


// Same Name Validation /////

	//Start For Existing Adgroup with both ad group i.e. with existing as well as with new added Ad group
for(j=1;j<counter;j++)
{
	for(k=1;k<counter;k++)
	{
		if(j!=k) {  //Existing with Existing Adgroup Validation
				if(document.getElementById("add_gr_name_"+j).value==document.getElementById("add_gr_name_"+k).value) {
							alert("Adgroup Name Should Be Unique");
							document.getElementById("add_gr_name_"+k).focus();
							return false;
						 }

			 }

		for(l=1;l<gFiles;l++) //Existing with New Added Adgroup Validation
			 {
				
				if(document.getElementById("createad"+l))
				{
					if(document.getElementById("add_gr_name_"+k).value == document.getElementById("add_gr_name"+l).value) {
							alert("Adgroup Name Should Be Unique all");
							document.getElementById("add_gr_name"+l).focus();
							return false;										}
				}

			 }
	}
}
	//End For Existing Adgroup with both ad group i.e. with existing as well as with new added Ad group

	// Start For New Added Adgroup validation between them

	for(m=1;m<gFiles;m++)
		{

		for(c=1;c<gFiles;c++) 
			{

			if(m!=c){
			if(document.getElementById("createad"+m)) {			
			if(document.getElementById("createad"+c))
			{
				if(document.getElementById("add_gr_name"+m).value == document.getElementById("add_gr_name"+c).value) {
						alert("Adgroup Name Should Be Unique");
						document.getElementById("add_gr_name"+c).focus();
						return false;										}
			}
								  }
				}
			}

		}

	// End For New Added Adgroup validation between them

// End Name Validation /////


// Numeric Validation
 
	if (IsNumeric(document.getElementById('total_budget').value) == false) 
      {
        alert("Please enter Numeric field only in Total Budget");
	document.getElementById('total_budget').focus();	
	return false;
      }

 	if (IsNumeric(document.getElementById('daily_budget_main').value) == false) 
      {
        alert("Please enter Numeric field only in Daily Budget");
	document.getElementById('daily_budget_main').focus();	
	return false;
      }

// End of numeric Validation

// Start Greater value of Total budget validation

if(document.getElementById('daily_budget_main').value > document.getElementById('total_budget').value )
{
	alert('Please enter Daily Budget less than Total Budget');
	document.getElementById('daily_budget_main').focus();	
	return false;
}

// end Greater value of Total budget validation



create_campaign.submit();
}


function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


function custom_check(identity,counter,adgroupid)
{

var identity = identity;
var counter  = counter;

	if(document.getElementById('network_'+counter).value == 'custom') {
	
	window.open('updatebuildCustomNetwork.php?form_id='+identity+'&db=db&counter='+counter+'&adgroupid='+adgroupid,'popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false;
									}
	
}

function custom_check_dynamic(identity)
{

var identity = identity;

	
	if(document.getElementById('network'+identity).value == 'custom') {
	
	window.open('updatebuildCustomNetwork.php?form_id='+identity+'&db=dynamic','popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false;
									  }
}


</script>


<script language="javascript" src="JavaScripts/registration.js"></script>
<link href="css/global.css" rel="stylesheet" type="text/css">
<!--<link rel="stylesheet" href="css/style.css" type="text/css" />-->
<!--<form id="myAccountForm" name="myAccountForm" method="post" action="" onsubmit="return regformvalidation();">-->

<table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top" align="center"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td width="20%" valign="top"><?php include("left_pannel.php"); ?></td>
                    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder" valign="top">

<!-- Start main body -->

	<form name="create_campaign" method="POST" id="create_campaign" action="">
	<input name="help_update_main" id="help_update_main" value="help_update_main" type="hidden">		
	<input name="TOTAL_FILE" id="TOTAL_FILE" value="1" type="hidden">


	<table align="center">
		<TR>
			<TD align="center" width="50%" valign="top"><!-- LEFT TD -->
				<table width="400">
					<tr><TD><b><font size="2"><?php echo CAMPAIGN_DETAILS; ?></font></b></TD></tr>
					<TR>
						<TD><?php echo NAME; ?></TD>
						<TD><input type="text" name="camp_name" id="camp_name" value="<?=$row_retrieve_campaign['camp_name']?>" /></TD>
					</TR>
					
					<tr>
						<TD></TD>
						<TD style="display:none;" id="validate_camp_name"><?php echo ENTER_NAME; ?></TD>
					</TR>

					<TR>
						<TD><?php echo SPEND_MEMO; ?></TD>
						<TD align="left">
							<table align="left">
								<TR><TD><input type="radio" name="spend_memo" value="fast_possible" checked="<? if($row_retrieve_campaign['spend_memo']=='fast_possible'){?>true <? } ?> "></TD>
								<td><?php echo RUN_ADS_AS_FAST; ?></td>
								</TR>

								<TR><TD><input type="radio" name="spend_memo" value="all_day" checked="<? if($row_retrieve_campaign['spend_memo']=='all_day'){?>true <? } ?> "></td><td><?php echo RUN_ADS_THROUGHOUT_DAY; ?></td></TR>
							</table>
						</TD>
					</TR>
					
					
					<TR>
						<TD><?php echo TOTAL_BUDGET; ?>    $   </TD>
						<TD><inpUT value="<?=$row_retrieve_campaign['total_budget']?>" type="text" name="total_budget" id="total_budget"></TD>
					</TR>
					
					<tr>
						<TD></TD>
						<TD style="display:none;" id="validate_total_budget"><?php echo ENTER_TOTAL_BUDGET; ?></TD>
					</TR>

					<TR>
						<TD><?php echo DAILY_BUDGET; ?></TD>
						<TD><inpUT type="text" name="daily_budget_main" id="daily_budget_main" value="<?=$row_retrieve_campaign['daily_budget']?>"></TD>
					</TR>
					
					<tr>
						<TD></TD>
						<TD style="display:none;" id="validate_daily_budget_main"><?php echo ENTER_DAILY_BUDGET; ?></TD>
					</TR>

					<TR>
						<TD><?php echo START_DATE; ?></TD>
						<TD>
							<table>
							<TR>
								


<TD>

 
<input value="<?=$row_retrieve_campaign['start_date']?>" type="text" readonly name="start_date" id="start_date" onclick="displayCalendar(document.create_campaign.start_date,'yyyy/mm/dd',this)">

</TD>
								<td><?php echo START_END_HOUR; ?>
									<select name="start_date_hour">
			<opTION value="1" <? if($row_retrieve_campaign['start_hour']=='1'){?> selected="selected" <? } ?> >1</opTION>
			<opTION value="2" <? if($row_retrieve_campaign['start_hour']=='2'){?> selected="selected" <? } ?>>2</opTION>
			<opTION value="3" <? if($row_retrieve_campaign['start_hour']=='3'){?> selected="selected" <? } ?>>3</opTION>
			<opTION value="4" <? if($row_retrieve_campaign['start_hour']=='4'){?> selected="selected" <? } ?>>4</opTION>
			<opTION value="5" <? if($row_retrieve_campaign['start_hour']=='5'){?> selected="selected" <? } ?>>5</opTION>
			<opTION value="6" <? if($row_retrieve_campaign['start_hour']=='6'){?> selected="selected" <? } ?>>6</opTION>
			<opTION value="7" <? if($row_retrieve_campaign['start_hour']=='7'){?> selected="selected" <? } ?>>7</opTION>
			<opTION value="8" <? if($row_retrieve_campaign['start_hour']=='8'){?> selected="selected" <? } ?>>8</opTION>
			<opTION value="9" <? if($row_retrieve_campaign['start_hour']=='9'){?> selected="selected" <? } ?>>9</opTION>
			<opTION value="10" <? if($row_retrieve_campaign['start_hour']=='10'){?> selected="selected" <? } ?>>10</opTION>
			<opTION value="11" <? if($row_retrieve_campaign['start_hour']=='11'){?> selected= selected <? } ?>  >11</opTION>
			<opTION value="12" <? if($row_retrieve_campaign['start_hour']=='12'){?> selected="selected" <? } ?>>12</opTION>
			<opTION value="13" <? if($row_retrieve_campaign['start_hour']=='13'){?> selected="selected" <? } ?>>13</opTION>
			<opTION value="14" <? if($row_retrieve_campaign['start_hour']=='14'){?> selected="selected" <? } ?>>14</opTION>
			<opTION value="15" <? if($row_retrieve_campaign['start_hour']=='15'){?> selected="selected" <? } ?>>15</opTION>
			<opTION value="16" <? if($row_retrieve_campaign['start_hour']=='16'){?> selected="selected" <? } ?>>16</opTION>
			<opTION value="17" <? if($row_retrieve_campaign['start_hour']=='17'){?> selected="selected" <? } ?>>17</opTION>
			<opTION value="18"<? if($row_retrieve_campaign['start_hour']=='18'){?> selected="selected" <? } ?>>18</opTION>
			<opTION value="19"<? if($row_retrieve_campaign['start_hour']=='19'){?> selected="selected" <? } ?>>19</opTION>
			<opTION value="20" <? if($row_retrieve_campaign['start_hour']=='20'){?> selected="selected" <? } ?>>20</opTION>
			<opTION value="21" <? if($row_retrieve_campaign['start_hour']=='21'){?> selected="selected" <? } ?>">21</opTION>
			<opTION value="22" <? if($row_retrieve_campaign['start_hour']=='22'){?> selected="selected" <? } ?>>22</opTION>
			<opTION value="23" <? if($row_retrieve_campaign['start_hour']=='23'){?> selected="selected" <? } ?>>23</opTION>

									</select>
								</td>
							</TR>
							</table>
						</td>
					</TR>
					
					<tr>
						<TD></TD>
						<TD style="display:none;" id="validate_start_date"><?php echo ENTER_START_DATE; ?></TD>
					</TR>

					<TR>
						<TD><?php echo END_DATE; ?></TD>
						<TD>
							<table>
							<TR>
								
<TD>

 
<input value="<?=$row_retrieve_campaign['end_date']?>" type="text" readonly name="end_date" id="end_date" onclick="displayCalendar(document.create_campaign.end_date,'yyyy/mm/dd',this)">
<!--<inpUT type="text" name="start_date" id="start_date" />-->


</TD>	
	<td><?php echo START_END_HOUR; ?>
		<select name="end_date_hour">
			<opTION value="1" <? if($row_retrieve_campaign['end_hour']=='1'){?> selected="selected" <? } ?> >1</opTION>
			<opTION value="2" <? if($row_retrieve_campaign['end_hour']=='2'){?> selected="selected" <? } ?>>2</opTION>
			<opTION value="3" <? if($row_retrieve_campaign['end_hour']=='3'){?> selected="selected" <? } ?>>3</opTION>
			<opTION value="4" <? if($row_retrieve_campaign['end_hour']=='4'){?> selected="selected" <? } ?>>4</opTION>
			<opTION value="5" <? if($row_retrieve_campaign['end_hour']=='5'){?> selected="selected" <? } ?>>5</opTION>
			<opTION value="6" <? if($row_retrieve_campaign['end_hour']=='6'){?> selected="selected" <? } ?>>6</opTION>
			<opTION value="7" <? if($row_retrieve_campaign['end_hour']=='7'){?> selected="selected" <? } ?>>7</opTION>
			<opTION value="8" <? if($row_retrieve_campaign['end_hour']=='8'){?> selected="selected" <? } ?>>8</opTION>
			<opTION value="9" <? if($row_retrieve_campaign['end_hour']=='9'){?> selected="selected" <? } ?>>9</opTION>
			<opTION value="10" <? if($row_retrieve_campaign['end_hour']=='10'){?> selected="selected" <? } ?>>10</opTION>
			<opTION value="11" <? if($row_retrieve_campaign['end_hour']=='11'){?> selected= selected <? } ?>  >11</opTION>
			<opTION value="12" <? if($row_retrieve_campaign['end_hour']=='12'){?> selected="selected" <? } ?>>12</opTION>
			<opTION value="13" <? if($row_retrieve_campaign['end_hour']=='13'){?> selected="selected" <? } ?>>13</opTION>
			<opTION value="14" <? if($row_retrieve_campaign['end_hour']=='14'){?> selected="selected" <? } ?>>14</opTION>
			<opTION value="15" <? if($row_retrieve_campaign['end_hour']=='15'){?> selected="selected" <? } ?>>15</opTION>
			<opTION value="16" <? if($row_retrieve_campaign['end_hour']=='16'){?> selected="selected" <? } ?>>16</opTION>
			<opTION value="17" <? if($row_retrieve_campaign['end_hour']=='17'){?> selected="selected" <? } ?>>17</opTION>
			<opTION value="18"<? if($row_retrieve_campaign['end_hour']=='18'){?> selected="selected" <? } ?>>18</opTION>
			<opTION value="19"<? if($row_retrieve_campaign['end_hour']=='19'){?> selected="selected" <? } ?>>19</opTION>
			<opTION value="20" <? if($row_retrieve_campaign['end_hour']=='20'){?> selected="selected" <? } ?>>20</opTION>
			<opTION value="21" <? if($row_retrieve_campaign['end_hour']=='21'){?> selected="selected" <? } ?>">21</opTION>
			<opTION value="22" <? if($row_retrieve_campaign['end_hour']=='22'){?> selected="selected" <? } ?>>22</opTION>
			<opTION value="23" <? if($row_retrieve_campaign['end_hour']=='23'){?> selected="selected" <? } ?>>23</opTION>
		</select>
	</td>
							</TR>
							</table>
						</td>
					</TR>

				</table>

				
	



			</TD>

			<TD align="center" valign="top"><!-- RIGHT TD START  -->
			<table>
				<TR><TD>
				<table>
				<TR><TD><?php echo AD_GROUPS; ?>  <?php echo WHAT_THIS; ?></TD></TR>
				<TR><TD><?php echo AD_GROUPS; ?>:</TD></TR>
				<TR><TD><?php echo ADS; ?></TD></TR>
				<TR><TD><input type="button" name="create_ad_group" onclick="return adddiv('<?php echo $optionstring;?>');" value="<?php echo CREATE_AD_GROUP; ?>"></TD></TR>
				</table>
				</td></tr>

				<TR><TD>
				

				<?php 
				
				$tot_ad = 1;
				while($row_retrieve_adgroup = mysql_fetch_array($res_retrieve_adgroup)){ 
		
			//	echo '<pre>';print_r($row_retrieve_adgroup);echo '</pre>';
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11" /></td>
        <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11" /></td>
        <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11" /></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td class="PinkTwosideBorder" align="center"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td align="center"><table name="createad_<?=$tot_ad?>" method="post" id="createad_<?=$tot_ad?>" action="create_campaign.php">
				<input name="help_insert" id="help_insert" value="help_insert" type="hidden">			
						<TR><TD><?php echo AD_TYPE; ?>: <?php echo AD_TEXT; ?></TD><td>	Units: CPC</td></TR>

						<TR><TD><?php echo AD_GROUPS_NAME; ?></TD><td><input type="text" name="add_gr_name_<?=$tot_ad?>" id="add_gr_name_<?=$tot_ad?>" value="<?=$row_retrieve_adgroup['adgroup_name']?>"></td></TR>
						<input type="hidden" name="add_gr_id_<?=$tot_ad?>" value="<?=$row_retrieve_adgroup['adgroup_id']?>">

						<TR><TD></TD><td id="validate_add_gr_name_<?=$tot_ad?>" style="display:none;"></td></TR>

						<TR><TD><?php echo DAILY_BUDGET; ?></TD><td><input type="text" name="daily_budget_<?=$tot_ad?>" id="daily_budget_<?=$tot_ad?>" value="<?=$row_retrieve_adgroup['adgroup_daily_budget']?>"></td></TR>
						<TR><TD><?php echo NETWORK; ?></TD><td>
						<select name="network_<?=$tot_ad?>" id="network_<?=$tot_ad?>" onchange="return custom_check(<?=$row_retrieve_adgroup['adgroup_network']?>,<?=$tot_ad?>,<?=$row_retrieve_adgroup['adgroup_id']?>);">
							<option value="Select Network">Select Network</option>

							<option value="custom" <?php if($row_retrieve_adgroup['network_type'] == 'custom'){ ?> selected = 'selected' <? } ?> >Custom</option>
			<? 
$sqlNetworkAds2 = "select * from ".NETWORKMASTER.",".NETWORKDETAILS." where ".NETWORKMASTER.".status='Y' AND ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId";

$execNetworkAds2 = mysql_query($sqlNetworkAds2);
			while($fetNetworkAds2 = mysql_fetch_array($execNetworkAds2))
			{ ?>			<option value="<?php echo $fetNetworkAds2['networkId'];?>" 
				<? if($row_retrieve_adgroup['adgroup_network'] == $fetNetworkAds2['networkId'] && $row_retrieve_adgroup['network_type'] == 'direct' ){ echo 'selected';  } ?> >
							
							<?php echo strtoupper($fetNetworkAds2[networkType]); ?>-<?php echo ucfirst($fetNetworkAds2[networkName]); ?>
							
							</option>
			<? } ?>
						</select>

						</td></TR>

						<TR><TD></TD><td id="validate_network_<?=$tot_ad?>" style="display:none;"></td></TR>

<?php 
$query_retrieve_adgroup_ad = "Select * from adgroup_ad where adgroup_id = ".$row_retrieve_adgroup['adgroup_id']." "; 
$res_retrieve_adgroup_ad = mysql_query($query_retrieve_adgroup_ad);

if(mysql_num_rows($res_retrieve_adgroup_ad)>0){
$ij=1;
while($row_retrieve_adgroup_ad = mysql_fetch_array($res_retrieve_adgroup_ad )) {
?>
					
<tr>
<td colspan="2" id="ad_adgrp_<?=$row_retrieve_adgroup_ad['adgroup_ad_id']?>">
<table style="color:#000000" cellpaddin="0" cellspacing="0" border="1" width="100%">
<TR>

<td style="cursor:pointer;" onclick="window.open('ad_adgroup_update.php?ad_id=<?=$row_retrieve_adgroup_ad['adgroup_ad_id']?>','popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false"><font size="2" color="Black"><?=$ij?>.</font>
</td>

<td style="cursor:pointer;" onclick="window.open('ad_adgroup_update.php?ad_id=<?=$row_retrieve_adgroup_ad['adgroup_ad_id']?>','popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false"><font size="2" color="Black"><?=$row_retrieve_adgroup_ad['personal_reference']?></font></td>

<td style="cursor:pointer;" onclick="window.open('ad_adgroup_update.php?ad_id=<?=$row_retrieve_adgroup_ad['adgroup_ad_id']?>','popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false"><font size="2" color="Black"><?=$row_retrieve_adgroup_ad['heading']?></font>
</td>

</tr>
</table>
</td>
</tr>

<? $ij++;} } else { ?>

						<TR><TD></TD><td style="display:none" id="main_adgroup_save_<?=$tot_ad?>"></td></TR>	
	<?  } ?>		
					
						<TR><TD id="tdCreateAd_<?=$tot_ad?>" colspan="2"></TD></TR>

						<TR><TD></TD><td><input type="button" name="create_ad_<?=$tot_ad?>" id="create_ad_<?=$tot_ad?>" value="<?php echo CREATE_AD; ?>" onclick="window.open('advertiser_direct_update.php?form_id=<?=$tot_ad?>','popup','width=800,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=50'); return false"></td></TR>

					
				</table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11" /></td>
        <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11" /></td>
        <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11" /></td>
      </tr>
    </table></td>
  </tr>
</table>
				<table><TR><TD height="10px"></TD></TR></table> <?php $tot_ad++;
				 } ?>
				<inpUT type="hidden" value="<?=$tot_ad?>" name="total_adgroup" id="total_adgroup" >
				<div id="adgroup">
				<td></td>
				</div>
</td></tr></table>
			</td><!-- RIGHT TD -->

		</TR>

<tr><TD><inpUT type="button" value="<?php echo UPDATE; ?>" id="save_all" name="save_all" onclick="return validate_main_campaign();"></TD><TD><inpUT type="submit" value="<?php echo CANCEL; ?>" id="cancel" name="cancel"></TD></tr>


</table>	
</form>



<!-- End main Body -->



			</td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
<!--</form>-->
            
<?php
        //include("footer.php");
}
		?> 